package com.ruoyi.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.admin.domain.entity.InventoryModel;
import com.ruoyi.admin.domain.vo.response.inventory.InventoryPageResponse;
import com.ruoyi.admin.domain.vo.response.inventory.InventoryResponse;
import com.ruoyi.admin.mapper.InventoryMapper;
import com.ruoyi.admin.service.InventoryService;
import com.ruoyi.common.api.Result;
import com.ruoyi.common.api.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存(Inventory)Service实现类
 *
 * @author jianhan
 * @date 2022-12-07 15:41:38
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, InventoryModel> implements InventoryService {

    private static final Logger logger = LoggerFactory.getLogger(InventoryServiceImpl.class);

    @Override
    public Result<InventoryResponse> getId(Long inventoryId) {
        InventoryModel inventoryModel = this.getById(inventoryId);
        if (inventoryModel != null) {
            InventoryResponse inventoryResponse = new InventoryResponse(inventoryModel);
            return Result.success(inventoryResponse);
        }
        return Result.failed(ResultCode.DATA_NOT_EXIST);
    }

    @Override
    public Result<InventoryPageResponse> page(Page<InventoryModel> page, InventoryModel searchModel) {
        QueryWrapper<InventoryModel> queryWrapper = queryWrapper(searchModel);

        Page<InventoryModel> pages = this.page(page, queryWrapper);
        InventoryPageResponse inventoryPageResponse = new InventoryPageResponse(pages);
        page.getRecords().forEach((inventoryModel) -> inventoryPageResponse.getRecords().add(new InventoryResponse(inventoryModel)));
        return Result.success(inventoryPageResponse);

    }

    @Override
    public Result add(InventoryModel inventoryModel) {
        Boolean result = this.save(inventoryModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("添加失败");
    }

    @Override
    public Result update(InventoryModel inventoryModel) {
        Boolean result = this.updateById(inventoryModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("更新失败");
    }

    @Override
    public Result delete(Long inventoryId) {
        Boolean result = this.removeById(inventoryId);
        return Result.judge(result);

    }

    private QueryWrapper<InventoryModel> queryWrapper(InventoryModel inventoryModel) {
        QueryWrapper<InventoryModel> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(inventoryModel.getBrand())) {
            queryWrapper.like("brand", inventoryModel.getBrand());
        }
        if (StringUtils.isNotEmpty(inventoryModel.getAtricleNumber())) {
            queryWrapper.like("atricle_number", inventoryModel.getAtricleNumber());
        }
        if (StringUtils.isNotEmpty(inventoryModel.getSize())) {
            queryWrapper.like("size", inventoryModel.getSize());
        }
        /*
           写搜索条件
        */
        queryWrapper.orderByDesc("id");
        queryWrapper.orderByDesc("update_time");
        queryWrapper.orderByDesc("total_price");
        queryWrapper.orderByDesc("number");
        return queryWrapper;
    }

}
